package image;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
public class SentEmail{
	public SentEmail(){}
	public void sendEmail(byte[] newImageData, String addrSender, String addrReceiver){
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props,null);
		String msgBody = "...";
		try {
            // Create a new message.
            Message msg = new MimeMessage(session);
            try {
				msg.setFrom(new InternetAddress(addrSender, "Nguyen Ky"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
            try {
				msg.addRecipient(Message.RecipientType.TO,
				                 new InternetAddress(addrReceiver, "Anyone"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
            msg.setSubject("Here's the Subject");
            msg.setText("hello");

            Multipart multipart = new MimeMultipart();

            // Create the message part.
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(msgBody);
            multipart.addBodyPart(messageBodyPart);

            // Part two is the attachment.
            MimeBodyPart attachment = new MimeBodyPart();
            attachment.setFileName("yourimage.png");
            ByteArrayDataSource dataSource = new ByteArrayDataSource(newImageData, "image/png"); 
            attachment.setDataHandler(new DataHandler(dataSource));
            multipart.addBodyPart(attachment);

            // Put parts in message
            msg.setContent(multipart);
            msg.saveChanges();
            
            // Send our message.
            Transport.send(msg);
        } catch (AddressException e) {
            return;
        } catch (MessagingException e) {
            return;
        }
	}
}